//JavaScript
var registerSession;
var articleList;
var curentArticleList;
var curentIdSection;
var nbArticles;
var nbPages;
var currentPage;
var curentNbPages;
var currentSecond;
var respArtile;

function setRegisterSession(registerSessionToSet){
  this.registerSession = registerSessionToSet;
}

function gestionException(xhr,exception){

$('#erreur').html("<b>"+exception.message + "</b><br>" +exception.stack+ "<br><br> Pour le message original suivant : <br>"+xhr.responseText);
}

function gestionReponseArticles(reponse)
{
    respArticle = true;
    nbArticles = reponse[0];
    var articles = reponse[1];
    articleList = articles;
    curentArticleList = articleList;
    if (articleList){
      if (articleList.length > 0)
      {
          nbPages = Math.ceil(nbArticles / 5);
          curentNbPages = nbPages;
          afficheEnteteArticles();
          var limit = Math.min(articleList.length, 5);
          var initValueCompt = 0;
          afficheFourchetteArticles(initValueCompt, limit);
      }
    }
    initTB();
}

function gestionReponseCategories(categories)
{
    if (categories){
      if (categories.length > 0)
      {
          $('#categories').html(afficheCategories(categories));
      }
    }
}

function getArticles(startPos,idSection, numberPage, idArticle){
  respArticle = false;
  curentIdSection = idSection;
  currentPage = numberPage;
  var url = 'include/bd/articles_bd.php';
  $.getJSON(url
        ,{typeRequete:'articles'
        , start_pos:startPos
        , id_section:idSection
        , id_article:idArticle}
        ,gestionReponseArticles);
  setTimeout(function(){
    if (!respArticle){
      $('#articles').html("<DIV ALIGN='center'><IMG SRC='images/loading.gif' ALIGN='center'></DIV>");
    }
  },100);
  
}

function getCategories(){
  var url = 'include/bd/articles_bd.php';
  $.getJSON(url
      ,{typeRequete:'sections'}
      ,gestionReponseCategories);
}

function changePage(numPage){
  var initValueCompt = (numPage * 5) ;
  getArticles(initValueCompt,curentIdSection, numPage,-1);
}


function filtreArticles(idSection){
  getArticles(0,idSection,-1);
  
  // On met à jour la combo en fonction de l'id sélectionné
  var optionList = $('#idSection')[0].options;
  for (var i =0; i < optionList.length; i++){
    var option = optionList[i];
    if (option.value == idSection){
      option.selected = true;
      break;
    }
  }
 
}

function afficheEnteteArticles(){
   var html = "";
  if (curentNbPages > 1){
    //CREATION DU SQUELETE HTML
    html = ""+
    "<TABLE WIDTH='95%'>"+
    	"<TD align='right'>"+
    	"Aller à la page";
    	// INJECTATION DES DONNEES
    		for (var i=0 ; i < curentNbPages ; i++)
        {
          if (i != 0){
            html += ", ";
          }else{
            html += " ";
          }
          if (i == currentPage){
            html += (i+1);
          }else{
            html += "<A class='blanc' HREF='javascript:changePage("+(i)+");'>"+(i+1)+"</A>";
          }  
        }
      // Poursuite du squelette HTML
      html += ""+      
    "</TD>"+
    "</TABLE>";
  }
  $('#enteteArticles').html(html);
}

function afficheFourchetteArticles(initComptValue, maxComptValue){
  var str = "";
  for (var i=initComptValue ; i < maxComptValue ; i++){
      var article = curentArticleList[i];
      str += afficheArticle(article);
  }
  $('#articles').html(str);
}


function afficheArticle(article){
//CREATION DU SQUELETE HTML
var html = ""+
"<DIV id='article_top'>"+
  "<TABLE WIDTH='800px'>"+
    "<TR>"+
      "<TD  style=' align:right; width:700px; color:white;' >"+
        "<A HREF='javascript:filtreArticles("+article.idSec+");' class='categorie'>"+html_entity_decode(article.nomSection)+"</A> : "+
        "<B>"+html_entity_decode(article.titre)+"</B>"+
      "</TD>"+
      "<TD style='text-align:left; margin-right:20px; color:white;'>"+
		    "<I> "+article.date+"</I></DIV>"+
		  "</TD>"+
		"</TR>"+
	"</TABLE>"+
"</DIV>"+
"<DIV id='article_content_back'>"+
	"<DIV id='article_content'>"+
		"<TABLE>"+
		"<TR>";
		// INJECTIONS DES DONNEES
			if (article.illustrations.length > 0 ){
			  html += ""+
			  "<TD valign='top' width='550px'>"+html_entity_decode(article.texte)+
			  "</TD>"+			
  			"<TD width='200px'>"+
  				"<TABLE>";
  				  for (var i = 0; i < article.illustrations.length; i++){
  				   var ID = ""+article.idArt+i;
  				   var illustration = article.illustrations[i];
  				   html += ""+
            "<TR>"+
  						"<TD align='center' width='200px'>"+
  						  getLinkImage(illustration, ID)+								
  						"</TD>"+
  					"</TR>";
            }
  			  html += ""+		
  				"</TABLE>"+
  			"</TD>";
			}else{
				html += ""+
			"<TD valign='top'>"+html_entity_decode(article.texte)+"</TD>";
			}
			// FIN DU SQUELETTE D'ARTICLE
			html += ""+
			"</TR>"+
		"</TABLE>"+
	"</DIV>"+
"</DIV>"+
"<DIV id='article_content_end' />"+
"<DIV id='article_bottom'>";
if (registerSession == 1) {
        html +=""+ 
				"<A class='blanc' HREF='index.php?page=update_article&idart="+article.idArt+"'>Editer cet article</A> -"; 
			}
			if (article.nbCommentaire > 0) {
        html += ""+ 
			"<A class='blanc' HREF='index.php?page=disp_comments&idart="+article.idArt+"'>"+article.nbCommentaire+" commentaire(s)</A> - <A class='blanc' HREF='index.php?page=add_comment&idart="+article.idArt+"'>Poster un commentaire</A>";
			} else { 
			 html += ""+
			"Pas de commentaire - <A class='blanc' HREF='index.php?page=add_comment&idart="+article.idArt+"'>Poster un commentaire</A>";
			}
html+=""+
"</DIV>"+
"<BR />";

return html;
}

function afficheCategories(categories){
//CREATION DU SQUELETE HTML
var html = ""+

"<TABLE >"+
	"<TD >Catégorie  </TD>"+
	"<TD WIDTH='10px' />"+
	"<TD align='left'>"+
	"<SELECT ID='idSection' onChange='javascript:filtreArticles(this.value);'  name='idsec'>"+
		"<OPTION value='*'>Toutes</OPTION>";
		// INJECTATION DES DONNEES
    for (var i=0; i < categories.length; i++){
      var categorie = categories[i];
      html += "<OPTION value='"+categorie.idSection+"'>"+html_entity_decode(categorie.nomSection)+"</OPTION>";
    }
    // Poursuite du squelette HTML
		html += ""+		
	"</SELECT>"+
	"</TD>"+
"</TABLE>";

return html;
}

function doAClick(link){
  if (canDo()){
    $("#"+link).click();
    var img = $('#TB_window'); 
    img.bind('mouseout', function(e){
      closeTB();
    });
  }
}

function canDo(){
  var result = false;
  var secondTemp = new Date().getTime();  
  if (((currentSecond != null) && ((secondTemp - currentSecond) > 1000))
    || (currentSecond == null)){
    currentSecond = secondTemp;
    result = true;
  }
  return result;
}

function closeTB(){
  if (canDo()){
    tb_remove();
  }
}
